home *** CD-ROM | disk | FTP | other *** search
- Path: news.slip.net!not-for-mail
- From: terman@slip.net (James L. Terman)
- Newsgroups: comp.lang.c++
- Subject: Help with managed resource class
- Date: Sat, 27 Jan 1996 17:25:45 -0800
- Message-ID: <4eej3r$9in@slip.net>
- NNTP-Posting-Host: sj-pm1-252.slip.net
- NNTP-NEWS-ADMIN: newsadmin@slip.net
-
- I was developing a binary tree class with a class called BEntry. Since every
- node of a binary tree is a subtree, I thought it would be best that every node
- owned its subnodes. If a node was deleted, it's subnode would be deleted as
- well. I defined two classes, HeapPtr_BEntry and BEntry:
-
- #include <iostream.h>
-
- class BEntry;
-
- class HeapPtr_BEntry
- {
- public:
- HeapPtr_BEntry(BEntry* ptr = NULL) : ptr_(ptr) {}
-
- void deallocate() { delete ptr_; ptr_ = NULL; }
- ~HeapPtr_BEntry() { deallocate(); }
-
- HeapPtr_BEntry& operator= (BEntry* ptr)
- { deallocate(); ptr_ = ptr; return *this; }
-
- operator BEntry*() const { return ptr_; }
- BEntry* operator->() const { return ptr_; }
- BEntry& operator*() const { return *ptr_; }
-
- BEntry *grab() { BEntry *old = ptr_; ptr_ = NULL; return old; }
-
- private:
- BEntry *ptr_;
- HeapPtr_BEntry operator= (const HeapPtr_BEntry&); // unimplemented
- HeapPtr_BEntry (const HeapPtr_BEntry&); // unimplememted
- };
-
- class BEntry
- {
- private:
- int* value_; // pointer to value_
- HeapPtr_BEntry leftEntry_; // remotely owned left node
- HeapPtr_BEntry rightEntry_; // remotely owned right node
- BEntry* prevEntry_; // pointer to parent node
-
- BEntry(const BEntry&); // make copy constructor and
- // assignment operatore private
- BEntry& operator=(const BEntry&); // so that BEntry is only created
- // dynamically
-
- public:
- BEntry() : prevEntry_(NULL) {}
- BEntry(int* value) : prevEntry_(NULL), value_(value) {}
- ~BEntry() {}
-
- void setValue(int* value) { value_ = value; }
- const int& getValue() const { return *value_; }
-
- int leftEmpty() { return !leftEntry_; }
- int rightEmpty() const { return !rightEntry_; }
- int prevEmpty() const { return !prevEntry_; }
- };
-
- Note that HeapPtr is defined through operator overloading so that
- functionally it acts just like a pointer. However, it seems to have
- trouble with the way
- a pointer is used in a if expression as it is in leftEmpty(), rightEmpty() and
- prevEmpty().
-
- When these two classes are compiled the compiliers complains:
- error: BEntry::rightEmpty() cannot access
- HeapPtr_BEntry::HeapPtr_BEntry(): private member
-
- Note that this does not happen with leftEmpty which was not declared a
- const function to demonstrate what's going wrong. prevEmpty is acting on
- an ordinary pointer and does not return an error. There is no way that the
- ! operator should
- change the value its acting upon and it certainly does not need to call the
- constructor function. Note this is not just a problem associated with the !
- operator, if (rightEntry_) return 1; else return 0; causes the same problem.
-
- This is not a disasterous problem. The following definition for rightEmpty
- int rightEmpty() const { return rigtEntry_==NULL; }
- compiles and works just fine. And of course, making rightEmpty a non-const
- function also works. I was just wondering if anybody could explain the
- compiler (MPW 3.2 C++) behavior.
- --
- | James L. Terman | Science may set limits to know- |
- | terman@slip.net | ledge, but should not set limits |
- | jlterman@aol.com | to imagination. |
- | http://www.slip.net/~terman | - Bertrand Russell |
-